<?php
namespace App\Http\Controllers\Shop;

use App\Http\Controllers\Controller;
use App\Jobs\PushMsgJob;
use App\Jobs\SendSystemMsgJob;
use App\Jobs\ShopTransJob;
use App\Jobs\VerifyCouponJob;
use App\Libraries\Decorator\Image\ImagesDecorator;
use App\Libraries\Decorator\Image\NoImagesDecorator;
use App\Libraries\Decorator\Tool\IsEndDecorator;
use App\Libraries\Decorator\User\UserPointDecorator;
use App\Libraries\Tools\CheckAuth;
use App\Libraries\Tools\ConstVar;
use App\Libraries\Tools\Msg;
use App\Libraries\Tools\Point;
use App\Libraries\Tools\Tools;
use App\Models\Shop\Shop;
use App\Models\Shop\ShopComment;
use App\Models\Shop\ShopContent;
use App\Models\Shop\ShopCoupon;
use App\Models\Shop\ShopCouponPurchase;
use App\Models\Shop\ShopCouponStatistic;
use App\Models\Shop\ShopFav;
use App\Models\Shop\ShopGirl;
use App\Models\Shop\ShopManager;
use App\Models\Shop\ShopService;
use App\Models\Shop\ShopStatistic;
use App\Models\Tag\GirlTag;
use App\Models\Tool\BadWord;
use App\Models\User\Balance;
use App\Models\User\User;
use Dingo\Api\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use App\Libraries\Decorator\Image\CoverDecorator;
use OSS\OssClient;
use OSS\Core\OssException;


class ShopController extends Controller{


    protected $shop;
    protected $shop_detail;
    protected $shop_comment;
    protected $cover_decorator;
    protected $images_decorator;
    protected $comment_decorator;
    protected $is_end_decorator;
    protected $no_images_decorator;

    public function __construct(Shop $shop, CoverDecorator $cover_decorator,
                                ImagesDecorator $images_decorator,
                                NoImagesDecorator $no_images_decorator,
                                IsEndDecorator $is_end_decorator){
        $this->shop = $shop;
        $this->cover_decorator = $cover_decorator;
        $this->images_decorator = $images_decorator;
        $this->no_images_decorator = $no_images_decorator;
        $this->is_end_decorator = $is_end_decorator;
    }

    /**
     * 商家列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function shops(Request $request){
        $validator = Validator::make(
            $request->all(), [
            'user_lng' => 'required|numeric',
            'user_lat' => 'required|numeric',
            'lng' => 'required_without_all:type,keyword,city_id,district_id,fav|numeric',
            'lat' => 'required_without_all:type,keyword,city_id,district_id,fav|numeric',
            'lng_radius' => 'required_without_all:type,keyword,city_id,district_id,fav|numeric',
            'lat_radius' => 'required_without_all:type,keyword,city_id,district_id,fav|numeric',
            'city_id' => 'numeric',
            'district_id' => 'integer',
            'user_city_id' => 'numeric',
            'type' => 'in:0,1,2,3,4,5,12',
            'order' => 'required|in:1,2,3,4,5',//1-距离 2-评价 3-价格 4-智能 5-人气
            'keyword' => 'string',
            'distance' => 'numeric',
            'fav' => 'in:0,1',
            'page' => 'required|integer',
            'size' => 'required|integer',
        ], [
            'user_lng.required' => '10001',
            'user_lng.numeric' => '10002',
            'user_lat.required' => '10001',
            'user_lat.numeric' => '10002',
            'lng.required_without_all' => '10001',
            'lng.numeric' => '10002',
            'lat.required_without_all' => '10001',
            'lat.numeric' => '10002',
            'lng_radius.required_without_all' => '10001',
            'lng_radius.numeric' => '10002',
            'lat_radius.required_without_all' => '10001',
            'lat_radius.numeric' => '10002',
            'user_city_id.required' => '10001',
            'user_city_id.numeric' => '10002',
            'city_id.numeric' => '10002',
            'district_id.integer' => '10002',
            'type.in' => '10002',
            'order.required' => '10001',
            'order.in' => '10002',
            'keyword.string' => '10002',
            'distance.numeric' => '10002',
            'fav.in' => '10002',
            'page.required' => '10001',
            'page.integer' => '10002',
            'size.required' => '10001',
            'size.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        $this->shop->addDecorator($this->cover_decorator);
        $this->shop->addDecorator($this->no_images_decorator);
        $this->shop->addDecorator($this->is_end_decorator);
        $data = $this->shop->shops($request, Auth::id());
        return $this->sendSuccessResponse($data);
    }

    /**
     * 商家详情
     * @param $id
     * @param ShopManager $manager
     * @param ShopGirl $shopGirl
     * @return \Illuminate\Http\JsonResponse
     */
    public function shop($id, ShopManager $manager, ShopGirl $shopGirl){
        $validator = Validator::make([
            'id' => $id,
        ], [
            'id' => 'required|integer',
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        $this->shop->addDecorator($this->cover_decorator);
        $this->shop->addDecorator($this->images_decorator);
//        $this->shop_detail->addDecorator($this->comments_decorator);
        $manager_data = $manager->where('user_id', Auth::id())->where('shop_id', $id)->first();
        $data = $this->shop->detail($id, Auth::id());
        if(empty($manager_data)){
            $data['manager_status'] = -1;
        }else{
            $data['manager_status'] = $manager_data['status'];
        }
        $data['girls'] = $shopGirl->where('shop_id', $id)->where('status', 1)->count();
        return $this->sendSuccessResponse($data);
    }

    /**
     * 商家评论
     * @param ShopComment $shop
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function comments(ShopComment $shop, Request $request, $id){
        $this->shop_comment = $shop;
        $valid_data = $request->all();
        $valid_data['id'] = $id;
        $validator = Validator::make($valid_data, [
            'id' => 'required|integer',
            'page' => 'required|integer',
            'size' => 'required|integer',
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
            'page.required' => '10001',
            'page.integer' => '10002',
            'size.required' => '10001',
            'size.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        $this->shop_comment->addDecorator($this->cover_decorator);
        $this->shop_comment->addDecorator($this->is_end_decorator);
        $this->shop_comment->addDecorator(new UserPointDecorator());
        $data = $this->shop_comment->comments($id, $request['page'], $request['size']);
        return $this->sendSuccessResponse($data);
    }

    /**
     * 添加评论
     * @param ShopComment $shop
     * @param ShopStatistic $statistic
     * @param Request $request
     * @param BadWord $badWord
     * @param ShopCouponPurchase $shopCouponPurchase
     * @return \Illuminate\Http\JsonResponse
     */
    public function addComment(ShopComment $shop,ShopStatistic $statistic,
                               Request $request, BadWord $badWord, ShopCouponPurchase $shopCouponPurchase){
        $this->shop_comment = $shop;
        $validator = Validator::make(
            $request->all(), [
            'id' => 'required|integer',
            'comment' => 'required|string|max:500',
            'score' => 'required|in:1,2,3,4,5',
            'purchase_id' => 'required|integer',
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
            'score.required' => '10001',
            'score.in' => '10002',
            'comment.required' => '10001',
            'comment.string' => '10002',
            'comment.size' => '10002',
            'purchase_id.required' => '10001',
            'purchase_id.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        $shopCouponPurchase = $shopCouponPurchase->where('id', $request['purchase_id'])->first();
        if(empty($shopCouponPurchase)){
            return $this->sendFailResponse(400, 40400);
        }
        if($shopCouponPurchase->user_id != Auth::id() || $shopCouponPurchase->status != ConstVar::$PURCHASE_STATUS_USED){
            return $this->sendFailResponse(400, 40001);
        }
        if(!empty($request['comment'])){
            $request['comment'] = Tools::removeXSS($request['comment']);
        }
        if($badWord->checkBadWord($request['comment']) != 200){
            return $this->sendFailResponse(400, 40005);
        }

        $user = Auth::user();
        $this->shop_comment->add($request->id, $user->user_id, $request->comment, $request->score);
        $statistic->where('shop_id', $request->id)->increment('comment');
        $shopCouponPurchase->status = ConstVar::$PURCHASE_STATUS_COMMENTED;
        $shopCouponPurchase->save();
        return $this->sendSuccessResponse([]);
    }

    /**
     * 申请经理(已废弃)
     * @param Request $request
     * @param ShopManager $manager
     * @return \Illuminate\Http\JsonResponse
     */
    public function applyManager(Request $request, ShopManager $manager){
//        Log::debug(json_encode($request->all()));
        $validator = Validator::make(
            $request->all(), [
            'shop_id' => 'required|integer',
            'name' => 'required|string|max:100',
            'company' => 'required|string|max:100',
            'company_phone' => 'required|string|max:50',
        ], [
            'shop_id.required' => '10001',
            'shop_id.integer' => '10002',
            'name.required' => '10001',
            'name.string' => '10002',
            'name.size' => '10002',
            'company.required' => '10001',
            'company.string' => '10002',
            'company.size' => '10002',
            'company_phone.required' => '10001',
            'company_phone.string' => '10002',
            'company_phone.size' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        if(!empty($request['name'])){
            $request['name'] = Tools::removeXSS($request['name']);
        }
        if(!empty($request['company'])){
            $request['company'] = Tools::removeXSS($request['company']);
        }
        if(!empty($request['company_phone'])){
            $request['company_phone'] = Tools::removeXSS($request['company_phone']);
        }
        $manager->user_id = Auth::id();
        $manager->shop_id = $request['shop_id'];
        $manager->name = $request['name'];
        $manager->status = 0;
        $manager->company = $request['company'];
        $manager->company_phone = $request['company_phone'];
        $manager->save();
        return $this->sendSuccessResponse([]);
    }

    /**
     * 商家申请进驻(已废弃)
     * @param Request $request
     * @param ShopContent $content
     * @param ShopService $service
     * @param ShopStatistic $statistic
     * @return \Illuminate\Http\JsonResponse
     */
    public function applyShop(Request $request,
                              ShopContent $content,
                              ShopService $service,
                              ShopStatistic $statistic){
        $validator = Validator::make(
            $request->all(), [
            'type' => 'required|in:1,2,3,4,5,6,7,9,11,12',
            'title' => 'required|string|max:50',
            'consumption' => 'required|integer',
            'cover' => 'required|image',
            'images.*' => 'image',
            'content' => 'string',
            'services' => 'json',
            'phone' => 'required|string|max:50',
            'province_id' => 'required|integer',
            'city_id' => 'required|integer',
            'district_id' => 'required|integer',
            'address' => 'required|string|max:200',
            'longitude' => 'required|numeric',
            'latitude' => 'required|numeric',
        ], [
            'type.required' => '10001',
            'type.in' => '10002',
            'title.required' => '10001',
            'title.string' => '10002',
            'title.size' => '10002',
            'consumption.required' => '10001',
            'consumption.integer' => '10002',
            'cover.required' => '10001',
            'cover.image' => '10003',
            'images.*.image' => '10003',
            'content.string' => '10002',
            'services.json' => '10002',
            'phone.required' => '10001',
            'phone.string' => '10002',
            'phone.max' => '10002',
            'province_id.required' => '10001',
            'province_id.integer' => '10002',
            'city_id.required' => '10001',
            'city_id.integer' => '10002',
            'district_id.required' => '10001',
            'district_id.integer' => '10002',
            'address.required' => '10001',
            'address.string' => '10002',
            'address.size' => '10002',
            'longitude.required' => '10001',
            'longitude.numeric' => '10002',
            'latitude.required' => '10001',
            'latitude.numeric' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        if(!empty($request['title'])){
            $request['title'] = Tools::removeXSS($request['title']);
        }
        if(!empty($request['consumption'])){
            $request['consumption'] = Tools::removeXSS($request['consumption']);
        }
        if(!empty($request['phone'])){
            $request['phone'] = Tools::removeXSS($request['phone']);
        }
        if(!empty($request['address'])){
            $request['address'] = Tools::removeXSS($request['address']);
        }
        if(!empty($request['content'])){
            $request['content'] = Tools::removeXSS($request['content']);
        }

        DB::beginTransaction();
        try{
            $ossClient = new OssClient(env('ALIYUN_ID'), env('ALIYUN_KEY'), env('ALIYUN_ENDPOINT'));
            $bucket = env('ALIYUN_BUCKET');
            $images = [];
            $this->shop->title = $request['title'];
            $this->shop->status = 0;
            $this->shop->verify_status = 0;
            $this->shop->type = $request['type'];
            $this->shop->consumption = $request['consumption'];
            if($request->hasFile('cover') && $request->file('cover')->isValid()){
                $object = 'image/'.uniqid().'.'.$request->file('cover')->extension();
                $ossClient->uploadFile($bucket, $object, $request->file('cover')->getFileInfo());
                $this->shop->cover = $object;
            }
            if(!empty($request['images'])){
                foreach($request->file('images') as $v){
                    if($v->isValid()){
                        $object = 'image/'.uniqid().'.'.$v->extension();
                        $ossClient->uploadFile($bucket, $object, $v->getFileInfo());
                        $images[] = $object;
                    }
                }
            }
            if(!empty($images)){
                $this->shop->images = json_encode($images);
            }
            $this->shop->phone = $request['phone'];
            $this->shop->province_id = $request['province_id'];
            $this->shop->city_id = $request['city_id'];
            $this->shop->district_id = $request['district_id'];
            $this->shop->address = $request['address'];
            $this->shop->longitude = $request['longitude'];
            $this->shop->latitude = $request['latitude'];
            $this->shop->save();
            $content->shop_id = $this->shop->shop_id;
            $content->content = empty($request['content'])?'':$request['content'];
            $content->save();
            if(!empty($request['services'])){
                $services = json_decode($request['services'], true);
                foreach($services as $v){
                    $service->shop_id = $this->shop->shop_id;
                    $service->name = $v['name'];
                    $service->price = $v['price'];
                    $service->save();
                    $service = new ShopService();
                }
            }
            $statistic->shop_id = $this->shop->shop_id;
            $statistic->save();
            DB::commit();
            return $this->sendSuccessResponse(['shop_id'=>$this->shop->shop_id]);
        }catch(OssException $e){
            DB::rollBack();
            return $this->sendFailResponse(400,40003);
        }catch(\Exception $e){
            DB::rollBack();
            return $this->sendFailResponse(500,50000);
        }
    }

    /**
     * 修改商家
     * @param $id
     * @param Request $request
     * @param ShopContent $shopContent
     * @param ShopService $shopService
     * @return \Illuminate\Http\JsonResponse
     */
    public function editShop($id, Request $request, ShopContent $shopContent,
                             ShopService $shopService){
        $valid_data = $request->all();
        $valid_data['id'] = $id;
        $validator = Validator::make(
            $valid_data, [
            'id' => 'required|integer',
            'type' => 'in:1,2,3,4,5,6,7,9,11,12',
            'title' => 'string|max:50',
            'consumption' => 'integer',
            'cover' => 'image',
            'no_image' => 'integer',
            'images.*' => 'image',
            'content' => 'string',
            'services' => 'json',
            'phone' => 'string|max:50',
            'province_id' => 'integer',
            'city_id' => 'integer',
            'district_id' => 'integer',
            'address' => 'string|max:200',
            'longitude' => 'numeric',
            'latitude' => 'numeric',
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
            'type.in' => '10002',
            'title.string' => '10002',
            'title.size' => '10002',
            'consumption.integer' => '10002',
            'cover.image' => '10003',
            'no_image.integer' => '10002',
            'images.*.image' => '10003',
            'content.string' => '10002',
            'services.json' => '10002',
            'phone.string' => '10002',
            'phone.max' => '10002',
            'province_id.integer' => '10002',
            'city_id.integer' => '10002',
            'district_id.integer' => '10002',
            'address.string' => '10002',
            'address.size' => '10002',
            'longitude.numeric' => '10002',
            'latitude.numeric' => '10002',
        ]);
        if ($validator->fails()) {
			$key = array_keys($validator->errors()->messages());
			return $this->sendFailResponse(400, $validator->errors()->first(), $key[0]);
        }
        $shop = $this->shop->where('shop_id', $id)->first();
        $content = $shopContent->where('shop_id', $id)->first();
        if(empty($content)){
            $content = $shopContent;
            $content->shop_id = $id;
            $content->content = '';
            $content->save();
        }
        if(empty($shop)){
            return $this->sendFailResponse(400, 40400);
        }
        if(!CheckAuth::checkIfManager($id)){
            return $this->sendFailResponse(400, 40101);
        }
        DB::beginTransaction();
        try{
            $ossClient = new OssClient(env('ALIYUN_ID'), env('ALIYUN_KEY'), env('ALIYUN_ENDPOINT'));
            $bucket = env('ALIYUN_BUCKET');
            $images = [];
            if(!empty($request['title'])){
                $shop->title = Tools::removeXSS($request['title']);
            }
            if(!empty($request['type'])){
                $shop->type = $request['type'];
            }
            if(!empty($request['consumption'])){
                $shop->consumption = Tools::removeXSS($request['consumption']);
            }
            if(!empty($request['phone'])){
                $shop->phone = Tools::removeXSS($request['phone']);
            }
            if(!empty($request['province_id'])){
                $shop->province_id = $request['province_id'];
            }
            if(!empty($request['city_id'])){
                $shop->city_id = $request['city_id'];
            }
            if(!empty($request['district_id'])){
                $shop->district_id = $request['district_id'];
            }
            if(!empty($request['address'])){
                $shop->address = Tools::removeXSS($request['address']);
            }
            if(!empty($request['longitude'])){
                $shop->longitude = $request['longitude'];
            }
            if(!empty($request['latitude'])){
                $shop->latitude = $request['latitude'];
            }
            if(!empty($request['content'])){
                $content->content = Tools::removeXSS($request['content']);
                $content->save();
            }
            if(!empty($request['services'])){
            	Log::debug($request['services']);
                $services = json_decode($request['services'], true);
                $shopService->where('shop_id', $id)->delete();
                foreach($services as $v){
                    $shopService->shop_id = $id;
                    $shopService->name = $v['name'];
                    $shopService->price = $v['price'];
                    $shopService->save();
                    $shopService = new ShopService();
                }
            }
            if($request->hasFile('cover') && $request->file('cover')->isValid()){
                $object = 'image/'.uniqid().'.'.$request->file('cover')->extension();
                $ossClient->uploadFile($bucket, $object, $request->file('cover')->getFileInfo());
                $shop->cover = $object;
            }
            if(!empty($request['no_image'])){
                $shop->images = null;
            }else{
                if(!empty($request['images'])){
                    foreach($request->file('images') as $v){
                        if($v->isValid()){
                            $object = 'image/'.uniqid().'.'.$v->extension();
                            $ossClient->uploadFile($bucket, $object, $v->getFileInfo());
                            $images[] = $object;
                        }
                    }
                }
                if(!empty($images)){
                    $shop->images = json_encode($images);
                }
            }
            $shop->save();
            DB::commit();
            return $this->sendSuccessResponse([]);
        }catch(OssException $e){
            DB::rollBack();
            return $this->sendFailResponse(400,40003);
        }catch(\Exception $e){
            DB::rollBack();
            Log::debug($e->getMessage());
            return $this->sendFailResponse(500,50000);
        }

    }

    /**
     * 添加浏览量
     * @param $id
     * @param ShopStatistic $statistic
     * @return \Illuminate\Http\JsonResponse
     */
    public function addView($id, ShopStatistic $statistic){
        $validator = Validator::make(
            ['id' => $id], [
            'id' => 'required|integer',
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        $statistic->where('shop_id', $id)->increment('view');
        return $this->sendSuccessResponse([]);
    }

    /**
     * 收藏商家
     * @param $id
     * @param ShopFav $fav
     * @return \Illuminate\Http\JsonResponse
     */
    public function fav($id, ShopFav $fav){
        $valid_data['id'] = $id;
        $validator = Validator::make($valid_data, [
            'id' => 'required|integer',
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        $user_id = Auth::id();
        //todo 检查商家状态
        $fav_data = $fav->firstOrCreate(['user_id'=>$user_id, 'shop_id'=>$id]);
        $fav_data->status = intval($fav_data->status XOR 1);
        $fav_data->save();
        return $this->sendSuccessResponse(['status'=>$fav_data->status]);
    }

    /**
     * 添加商家优惠信息
     * @param $id
     * @param Request $request
     * @param ShopCoupon $shopCoupon
     * @param ShopCouponStatistic $shopCouponStatistic
     * @return \Illuminate\Http\JsonResponse
     */
    public function addCoupon($id, Request $request, ShopCoupon $shopCoupon,
                              ShopCouponStatistic $shopCouponStatistic){
        $valid_data = $request->all();
        $valid_data['id'] = $id;
        $validator = Validator::make(
            $valid_data, [
            'id' => 'required|integer',
            'title' => 'required|string|max:50',
            'content' => 'required|string|max:500',
            'cover' => 'required|image',
            'rec_num' => 'required|integer',
            'price' => 'required|numeric',
            'discount_price' => 'required|numeric',
            'start_at' => 'required|integer',
            'end_at' => 'required|integer',
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
            'title.required' => '10001',
            'title.string' => '10002',
            'title.max' => '10002',
            'content.required' => '10001',
            'content.string' => '10002',
            'content.max' => '10002',
            'cover.required' => '10001',
            'cover.image' => '10003',
            'rec_num.required' => '10001',
            'rec_num.integer' => '10002',
            'price.required' => '10001',
            'price.integer' => '10002',
            'discount_price.required' => '10001',
            'discount_price.integer' => '10002',
            'start_at.required' => '10001',
            'start_at.integer' => '10002',
            'end_at.required' => '10001',
            'end_at.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }

        if(!CheckAuth::checkIfManager($id)){
            return $this->sendFailResponse(400, 40101);
        }

        if($request['start_at'] >= $request['end_at']){
            if($request['start_at'] == $request['end_at']){
                $request['end_at'] += 86399;
            }else{
                return $this->sendFailResponse(400, 40001);
            }
        }
        if(empty(floatval($request['price']))){
            return $this->sendFailResponse(400, 40001);
        }
        if(empty(floatval($request['discount_price']))){
            return $this->sendFailResponse(400, 40001);
        }
        if($request['discount_price'] >= $request['price']){
            return $this->sendFailResponse(400, 40001);
        }

        if(!empty($request['title'])){
            $request['title'] = Tools::removeXSS($request['title']);
        }
        if(!empty($request['content'])){
            $request['content'] = Tools::removeXSS($request['content']);
        }

        try{
            $ossClient = new OssClient(env('ALIYUN_ID'), env('ALIYUN_KEY'), env('ALIYUN_ENDPOINT'));
            $bucket = env('ALIYUN_BUCKET');
            if($request->hasFile('cover') && $request->file('cover')->isValid()){
                $object = 'image/'.uniqid().'.'.$request->file('cover')->extension();
                $ossClient->uploadFile($bucket, $object, $request->file('cover')->getFileInfo());
                $shopCoupon->cover = $object;
            }
            $shopCoupon->shop_id = $id;
            $shopCoupon->title = $request['title'];
            $shopCoupon->content = $request['content'];
            $shopCoupon->rec_num = $request['rec_num'];
            $shopCoupon->price = $request['price'];
            $shopCoupon->discount_price = $request['discount_price'];
            $shopCoupon->start_at = $request['start_at'];
            $shopCoupon->end_at = $request['end_at'];
            $shopCoupon->save();
        }catch(OssException $e){
            return $this->sendFailResponse(400, 40003);
        }catch(\Exception $e){
            return $this->sendFailResponse(500, 50000);
        }
        $shopCouponStatistic->shop_id = $id;
        $shopCouponStatistic->coupon_id = $shopCoupon->id;
        $shopCouponStatistic->save();
        return $this->sendSuccessResponse([]);
    }

    /**
     * 修改套餐
     * @param $id
     * @param Request $request
     * @param ShopCoupon $shopCoupon
     * @return \Illuminate\Http\JsonResponse
     */
    public function editCoupon($id, Request $request, ShopCoupon $shopCoupon){
        $valid_data = $request->all();
        $valid_data['id'] = $id;
        $validator = Validator::make(
            $valid_data, [
            'id' => 'required|integer',
            'title' => 'string|max:50',
            'content' => 'string|max:500',
            'cover' => 'image',
            'rec_num' => 'integer',
            'price' => 'numeric',
            'discount_price' => 'numeric',
            'start_at' => 'required_with:end_at|integer',
            'end_at' => 'required_with:start_at|integer',
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
            'title.string' => '10002',
            'title.max' => '10002',
            'content.string' => '10002',
            'content.max' => '10002',
            'cover.image' => '10003',
            'rec_num.integer' => '10002',
            'price.integer' => '10002',
            'discount_price.integer' => '10002',
            'start_at.required_with' => '10001',
            'start_at.integer' => '10002',
            'end_at.required_with' => '10001',
            'end_at.integer' => '10002',
        ]);

        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }

        $shopCoupon = $shopCoupon->where('id', $id)->firstOrFail();

        if(!CheckAuth::checkIfManager($shopCoupon->shop_id)){
            return $this->sendFailResponse(400, 40101);
        }

        if(!empty($request['start_at']) && !empty($request['end_at'])){
            if($request['start_at'] >= $request['end_at']){
                return $this->sendFailResponse(400, 40001);
            }
            $shopCoupon->start_at = $request['start_at'];
            $shopCoupon->end_at = $request['end_at'];
        }

        if($request->hasFile('cover') && $request->file('cover')->isValid()){
            try{
                $ossClient = new OssClient(env('ALIYUN_ID'), env('ALIYUN_KEY'), env('ALIYUN_ENDPOINT'));
                $bucket = env('ALIYUN_BUCKET');
                $object = 'image/'.uniqid().'.'.$request->file('cover')->extension();
                $ossClient->uploadFile($bucket, $object, $request->file('cover')->getFileInfo());
                $cover_num = ShopCouponPurchase::where('cover',$shopCoupon->cover)->count();
                if($cover_num==0){
                    $ossClient->deleteObject($bucket, $shopCoupon->cover);
                }
                $shopCoupon->cover = $object;
            }catch (OssException $e){
                return $this->sendFailResponse(400,40003);
            }
        }

        if(!empty($request['title'])){
            $shopCoupon->title = Tools::removeXSS($request['title']);
        }
        if(!empty($request['content'])){
            $shopCoupon->content = Tools::removeXSS($request['content']);
        }
        if(isset($request['rec_num'])){
            $shopCoupon->rec_num = $request['rec_num'];
        }
        if(isset($request['price'])){
            if(empty(floatval($request['price']))){
                return $this->sendFailResponse(400, 40001);
            }
            $shopCoupon->price = $request['price'];
        }
        if(isset($request['discount_price'])){
            if(empty(floatval($request['discount_price']))){
                return $this->sendFailResponse(400, 40001);
            }
            $shopCoupon->discount_price = $request['discount_price'];
        }
        $shopCoupon->save();
        return $this->sendSuccessResponse($shopCoupon);
    }

    /**
     * 冻结套餐
     * @param $id
     * @param ShopCoupon $shopCoupon
     * @return \Illuminate\Http\JsonResponse
     */
    public function disableCoupon($id, ShopCoupon $shopCoupon){
        $valid_data['id'] = $id;
        $validator = Validator::make(
            $valid_data, [
            'id' => 'required|integer',
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
        ]);

        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }

        $shopCoupon = $shopCoupon->where('id', $id)->firstOrFail();

        if(!CheckAuth::checkIfManager($shopCoupon->shop_id)){
            return $this->sendFailResponse(400, 40101);
        }

        $shopCoupon->status = 0;
        $shopCoupon->save();
        return $this->sendSuccessResponse([]);
    }

    /**
     * 套餐列表
     * @param $id
     * @param ShopCoupon $shopCoupon
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function coupons($id, ShopCoupon $shopCoupon, Request $request){
        $valid_data = $request->all();
        $valid_data['id'] = $id;
        $validator = Validator::make($valid_data, [
            'id' => 'required|integer',
            'page' => 'required|integer',
            'size' => 'required|integer',
            'is_manager' => 'required|in:0,1',
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
            'page.required' => '10001',
            'page.integer' => '10002',
            'size.required' => '10001',
            'size.integer' => '10002',
            'is_manager.required' => '10001',
            'is_manager.in' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }

        $is_manager = $request['is_manager'] && CheckAuth::checkIfManager($id);

        $shopCoupon->addDecorator($this->cover_decorator);
        $shopCoupon->addDecorator($this->is_end_decorator);
        $data = $shopCoupon->coupons($id, $request['page'], $request['size'], $is_manager);
        return $this->sendSuccessResponse($data);
    }

    /**
     * 套餐详情
     * @param $id
     * @param ShopCoupon $shopCoupon
     * @return \Illuminate\Http\JsonResponse
     */
    public function coupon($id, ShopCoupon $shopCoupon){
        $valid_data['id'] = $id;
        $validator = Validator::make($valid_data, [
            'id' => 'required|integer',
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        $shopCoupon->addDecorator($this->cover_decorator);
        $shopCoupon->addDecorator($this->images_decorator);
        $data = $shopCoupon->detail($id, true, true);
        if(empty($data)){
            return $this->sendFailResponse(400, 40400);
        }
        return $this->sendSuccessResponse($data);
    }

    /**
     * 套餐订单详情
     * @param $code
     * @param ShopCouponPurchase $shopCouponPurchase
     * @return \Illuminate\Http\JsonResponse
     */
    public function couponPurchase($code, ShopCouponPurchase $shopCouponPurchase){
        $valid_data['code'] = $code;
        $validator = Validator::make($valid_data, [
            'code' => 'required|integer',
        ], [
            'code.required' => '10001',
            'code.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }

        $shopCouponPurchase->addDecorator($this->cover_decorator);
        $data = $shopCouponPurchase->getPurchaseByCode($code);
        return $this->sendSuccessResponse($data);
    }

    /**
     * 套餐订单列表
     * @param Request $request
     * @param ShopCouponPurchase $shopCouponPurchase
     * @param ShopManager $shopManager
     * @return \Illuminate\Http\JsonResponse
     */
    public function couponPurchases(Request $request, ShopCouponPurchase $shopCouponPurchase,
                                    ShopManager $shopManager){
        $valid_data = $request->all();
        if(!empty($request['status'])){
            $valid_data['status'] = \GuzzleHttp\json_decode($request['status'], true);
        }
        $validator = Validator::make($valid_data, [
            'page' => 'required|integer',
            'size' => 'required|integer',
            'status.*' => 'in:0,1,2,3,4,5',
            'status' => 'array',
            'shop_id' => 'integer',
            'start_at' => 'integer',
            'end_at' => 'integer',
        ], [
            'page.required' => '10001',
            'page.integer' => '10002',
            'size.required' => '10001',
            'size.integer' => '10002',
            'status.*.in' => '10002',
            'status.array' => '10002',
            'shop_id.integer' => '10002',
            'start_at.integer' => '10002',
            'end_at.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        if(!empty($request['shop_id'])){
            $manager = $shopManager::where('shop_id', $request['shop_id'])
                ->where('user_id', Auth::id())
                ->where('status', 1)
                ->first();
            if(empty($manager)){
                return $this->sendFailResponse(400, 40101);
            }
        }
        $shopCouponPurchase->addDecorator($this->cover_decorator);
        $shopCouponPurchase->addDecorator($this->is_end_decorator);

        $data = $shopCouponPurchase->getPurchases(
            $request['page'],
            $request['size'],
            $request['start_at']??null,
            $request['end_at']??null,
            $valid_data['status']??null,
            $request['shop_id']??null
        );
        return $this->sendSuccessResponse($data);
    }

    /**
     * 商家验证套餐
     * @param $code
     * @param ShopCouponPurchase $shopCouponPurchase
     * @param Balance $balance
     * @return \Illuminate\Http\JsonResponse
     */
    public function verifyCouponPurchase($code, ShopCouponPurchase $shopCouponPurchase, Balance $balance){
        $valid_data['code'] = $code;
        $validator = Validator::make($valid_data, [
            'code' => 'required|integer',
        ], [
            'code.required' => '10001',
            'code.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        $shopCouponPurchase = $shopCouponPurchase->where('code', $code)
            ->where('status', ConstVar::$PURCHASE_STATUS_UNUSED)
            ->first();
        if(empty($shopCouponPurchase)){
            return $this->sendFailResponse(400, 40001);
        }
        if(!CheckAuth::checkIfManager($shopCouponPurchase->shop_id)){
            return $this->sendFailResponse(400, 40101);
        }
        DB::beginTransaction();
        try{
            $shopCouponPurchase->status = 1;
            $shopCouponPurchase->verify_at = time();
            $shopCouponPurchase->save();

            $this->shop->where('shop_id', $shopCouponPurchase->shop_id)->increment('balance', $shopCouponPurchase->amount);

            $balance->add(//入账记录
                Msg::system_pay().'_'.Msg::balance_coupon_verify($shopCouponPurchase->coupon_id),
                0,
                $shopCouponPurchase->shop_id,
                ConstVar::$BALANCE_CHANNEL_BALANCE,
                ConstVar::$BALANCE_CHANNEL_BALANCE,
                ConstVar::$ORDER_TYPE_COUPON,
                $shopCouponPurchase->coupon_id,//coupon_id
                $shopCouponPurchase->amount,
                $shopCouponPurchase->id,
                0,//商家验证时候的红包先不管
                0,
                ConstVar::$BALANCE_STATUS_SUCCESS,
                ConstVar::$BALANCE_TYPE_SYSTEM,
                ConstVar::$BALANCE_TYPE_SHOP
            );
            ShopCouponStatistic::where('coupon_id', $shopCouponPurchase->coupon_id)->increment('sales');
            ShopStatistic::where('shop_id', $shopCouponPurchase->shop_id)->increment('sales');
            //添加积分
            (new Point())->addCouponPoint($shopCouponPurchase);
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            return $this->sendFailResponse(500, 50000);
        }

        dispatch(new VerifyCouponJob($shopCouponPurchase));

        $owner = ShopManager::where('shop_id', $shopCouponPurchase->shop_id)
            ->where('status', 1)->where('level', 1)->first();
        if(!empty($owner)){
            dispatch(new SendSystemMsgJob([$owner->user_id], Msg::transfer_to_shop($shopCouponPurchase->amount), 0));
            dispatch(new PushMsgJob([$owner->user_id], Msg::transfer_to_shop($shopCouponPurchase->amount), 0));
            dispatch(new ShopTransJob($shopCouponPurchase->order_id, $owner->user_id, $shopCouponPurchase->shop_id));
        }

        return $this->sendSuccessResponse([]);
    }

    /**
     * 经营数据
     * @param $id
     * @param Request $request
     * @param ShopCouponPurchase $shopCouponPurchase
     * @return \Illuminate\Http\JsonResponse
     */
    public function business($id, Request $request, ShopCouponPurchase $shopCouponPurchase){
        $valid_data = $request->all();
        $valid_data['id'] = $id;
        $validator = Validator::make($valid_data, [
            'id' => 'required|integer',
            'start_at' => 'required|integer',
            'end_at' => 'required|integer',
            'type' => 'required|in:1,2', //1-销售量 2-消费量
            'date_type' => 'required|in:1,2' //1-日 2-月
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
            'start_at.required' => '10001',
            'start_at.integer' => '10002',
            'end_at.required' => '10001',
            'end_at.integer' => '10002',
            'type.required' => '10001',
            'type.in' => '10002',
            'date_type.required' => '10001',
            'date_type.in' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        $data = $shopCouponPurchase->businessData($id, $request);
        return $this->sendSuccessResponse($data);
    }

    /**
     * 添加经理
     * @param Request $request
     * @param ShopManager $manager
     * @param User $user
     * @param Shop $shop
     * @return \Illuminate\Http\JsonResponse
     */
    public function addManager(Request $request, ShopManager $manager, User $user, Shop $shop){
        $validator = Validator::make(
            $request->all(), [
            'id' => 'required|integer',
            'name' => 'required|string|max:100',
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
            'name.required' => '10001',
            'name.string' => '10002',
            'name.size' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        $owner = CheckAuth::checkIfOwner(Auth::id(), $manager);
        if(!$owner){
            return $this->sendFailResponse(400, 40101);
        }
        $shop = $shop->where('shop_id', $owner->shop_id)->where('status', 1)->where('verify_status', 1)->first();
        if(!$shop){
            return $this->sendFailResponse(400, 40400);
        }
        $new_manager = $user->where('user_id', $request['id'])
            ->orWhere('mobile', $request['id'])
            ->first();
        if(!$new_manager){
            return $this->sendFailResponse(400, 40302);
        }
        if(CheckAuth::checkIfManager($owner->shop_id, $new_manager->user_id, true)){
            return $this->sendFailResponse(400, 40001);
        }

        $manager->user_id = $new_manager->user_id;
        $manager->shop_id = $owner->shop_id;
        $manager->name = Tools::removeXSS($request['name']);
        $manager->status = 1;
        $manager->level = 2;
        $manager->company = $owner->company;
        $manager->company_phone = $owner->company_phone;
        $manager->save();
        dispatch(
            new SendSystemMsgJob(
                [$new_manager->user_id],
                Msg::manager_earn($shop->title),
                ConstVar::$EASEMOB_MSG_TYPE_MANAGER,
                [
                    'user_id' => 6,
                    'type' => ConstVar::$EASEMOB_MSG_TYPE_MANAGER,
                    'manager' => 1,
                    'shop_id' => $owner->shop_id,
                    'shop_title' => $shop->title,
                ]
            )
        );
        dispatch(
            new PushMsgJob(
                [$new_manager->user_id],
                Msg::manager_earn($shop->title),
                ConstVar::$EASEMOB_MSG_TYPE_MANAGER,
                [
                    'user_id' => 6,
                    'type' => ConstVar::$EASEMOB_MSG_TYPE_MANAGER,
                    'manager' => 1,
                    'shop_id' => $owner->shop_id,
                    'shop_title' => $shop->title,
                ]
            )
        );
        return $this->sendSuccessResponse([]);
    }

    /**
     * 经理列表
     * @param Request $request
     * @param ShopManager $shopManager
     * @return \Illuminate\Http\JsonResponse
     */
    public function managers(Request $request, ShopManager $shopManager){
        $validator = Validator::make(
            $request->all(), [
            'shop_id' => 'required|integer',
        ], [
            'shop_id.required' => '10001',
            'shop_id.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }

        if(!CheckAuth::checkIfManager($request['shop_id'], Auth::id())){
            return $this->sendFailResponse(400, 40001);
        }

        $shopManager->addDecorator($this->cover_decorator);
        $data = $shopManager->allManagers($request['shop_id']);
        return $this->sendSuccessResponse($data);
    }

    /**
     * 修改经理
     * @param $id
     * @param Request $request
     * @param ShopManager $shopManager
     * @return \Illuminate\Http\JsonResponse
     */
    public function editManager($id, Request $request, ShopManager $shopManager){
        $vail_data = $request->all();
        $vail_data['id'] = $id;
        $validator = Validator::make(
            $vail_data, [
            'id' => 'required|integer',
            'status' => 'in:1,3',
            'name' => 'string|max:100',
            'display' => 'in:0,1',
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
            'status.in' => '10002',
            'name.required' => '10001',
            'name.string' => '10002',
            'name.size' => '10002',
            'display.in' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }

        $manager = $shopManager->where('id', $id)->first();
        if(empty($manager)){
            return $this->sendFailResponse(400, 40400);
        }

        $owner = CheckAuth::checkIfOwner(Auth::id(), $shopManager);
        if(empty($owner)){
            if(!CheckAuth::checkIfManager($manager->shop_id, Auth::id())){
                return $this->sendFailResponse(400, 40101);
            }
        }else{
            if($owner->shop_id != $manager->shop_id){
                return $this->sendFailResponse(400, 40101);
            }
        }

        $shop = $this->shop->where('shop_id', $manager->shop_id)->first();
        if(empty($shop)){
            return $this->sendFailResponse(400, 40400);
        }

        if(isset($request['status'])){
            $manager->status = $request['status'];
            $msg = $request['status']==1?Msg::manager_earn($shop->title):Msg::manager_lose($shop->title);
        }
        if(isset($request['name'])){
            $manager->name = Tools::removeXSS($request['name']);
        }
        if(isset($request['display'])){
            $manager->display = $request['display'];
        }
        $manager->save();
        if(isset($msg)){
            dispatch(
                new SendSystemMsgJob(
                    [$manager->user_id],
                    $msg,
                    ConstVar::$EASEMOB_MSG_TYPE_MANAGER,
                    [
                        'user_id' => 6,
                        'type' => ConstVar::$EASEMOB_MSG_TYPE_MANAGER,
                        'manager' => $request['status'],
                        'shop_id' => $owner->shop_id,
                        'shop_title' => $shop->title,
                    ]
                )
            );
            dispatch(
                new PushMsgJob(
                    [$manager->user_id],
                    $msg,
                    ConstVar::$EASEMOB_MSG_TYPE_MANAGER,
                    [
                        'user_id' => 6,
                        'type' => ConstVar::$EASEMOB_MSG_TYPE_MANAGER,
                        'manager' => $request['status'],
                        'shop_id' => $owner->shop_id,
                        'shop_title' => $shop->title,
                    ]
                )
            );
        }
        return $this->sendSuccessResponse($manager);
    }

    /**
     * 技师列表
     * @param $id
     * @param Request $request
     * @param ShopGirl $shopGirl
     * @return \Illuminate\Http\JsonResponse
     */
    public function girls($id, Request $request, ShopGirl $shopGirl){
        $vail_data = $request->all();
        $vail_data['id'] = $id;
        $validator = Validator::make(
            $vail_data, [
            'id' => 'required|integer',
            'page' => 'required|integer',
            'size' => 'required|integer',
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
            'page.required' => '10001',
            'page.integer' => '10002',
            'size.required' => '10001',
            'size.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        $shopGirl->addDecorator(new CoverDecorator());
        $shopGirl->addDecorator(new NoImagesDecorator());
        $shopGirl->addDecorator(new IsEndDecorator());
        $data = $shopGirl->getGirls($id, $request['page'], $request['size']);
        return $this->sendSuccessResponse($data);
    }

    /**
     * 添加技师
     * @param $id
     * @param Request $request
     * @param ShopGirl $shopGirl
     * @param GirlTag $girlTag
     * @return \Illuminate\Http\JsonResponse
     */
    public function addGirls($id, Request $request, ShopGirl $shopGirl, GirlTag $girlTag){
        $vail_data = $request->all();
        $vail_data['id'] = $id;
        $validator = Validator::make(
            $vail_data, [
            'id' => 'required|integer',
            'user_id' => 'integer',
            'no' => 'required|integer',
            'cover' => 'required|image',
            'age' => 'required|integer',
            'height' => 'required|integer',
            'weight' => 'required|integer',
            'bust' => 'required|integer',
            'waistline' => 'required|integer',
            'hips' => 'required|integer',
            'tags' => 'required|json',
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
            'user_id.integer' => '10002',
            'no.required' => '10001',
            'no.integer' => '10002',
            'cover.required' => '10001',
            'cover.image' => '10003',
            'age.required' => '10001',
            'age.integer' => '10002',
            'height.required' => '10001',
            'height.integer' => '10002',
            'weight.required' => '10001',
            'weight.integer' => '10002',
            'bust.required' => '10001',
            'bust.integer' => '10002',
            'waistline.required' => '10001',
            'waistline.integer' => '10002',
            'hips.required' => '10001',
            'hips.integer' => '10002',
            'tags.required' => '10001',
            'tags.json' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }

        if(!CheckAuth::checkIfManager($id)){
            return $this->sendFailResponse(400, 40101);
        }

        DB::beginTransaction();
        try{
            $ossClient = new OssClient(env('ALIYUN_ID'), env('ALIYUN_KEY'), env('ALIYUN_ENDPOINT'));
            $bucket = env('ALIYUN_BUCKET');

            $shopGirl->shop_id = $id;
            if(!empty($request['user_id'])){
                if(!CheckAuth::checkIfValidUser($request['user_id'])){
                    return $this->sendFailResponse(400,40400);
                }
                $shopGirl->user_id = $request['user_id'];
            }
            $shopGirl->no = $request['no'];
            if($request->hasFile('cover') && $request->file('cover')->isValid()){
                $object = 'image/'.uniqid().'.'.$request->file('cover')->extension();
                $ossClient->uploadFile($bucket, $object, $request->file('cover')->getFileInfo());
                $shopGirl->cover = $object;
            }
            $shopGirl->age = $request['age'];
            $shopGirl->height = $request['height'];
            $shopGirl->weight = $request['weight'];
            $shopGirl->bust = $request['bust'];
            $shopGirl->waistline = $request['waistline'];
            $shopGirl->hips = $request['hips'];
            $shopGirl->save();
            if(!empty($request['tags'])){
                $request['tags'] = \GuzzleHttp\json_decode($request['tags'], true);
                foreach($request['tags'] as $v){
                    $girlTag->girl_id = $shopGirl->id;
                    $girlTag->tag_id = $v;
                    $girlTag->save();
                    $girlTag = new GirlTag();
                }
            }
            DB::commit();
            return $this->sendSuccessResponse([]);
        }catch(OssException $e){
            DB::rollBack();
            return $this->sendFailResponse(400,40003);
        }catch(\Exception $e){
            DB::rollBack();
            return $this->sendFailResponse(500,50000);
        }
    }

    public function editGirls($id, Request $request, ShopGirl $shopGirl, GirlTag $girlTag){
        $vail_data = $request->all();
        $vail_data['id'] = $id;
        $validator = Validator::make(
            $vail_data, [
            'id' => 'required|integer',
            'user_id' => 'integer',
            'no' => 'integer',
            'cover' => 'image',
            'age' => 'integer',
            'status' => 'in:0,1',
            'height' => 'integer',
            'weight' => 'integer',
            'bust' => 'integer',
            'waistline' => 'integer',
            'hips' => 'integer',
            'tags' => 'json',
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
            'user_id.integer' => '10002',
            'no.required' => '10001',
            'no.integer' => '10002',
            'cover.required' => '10001',
            'cover.image' => '10003',
            'age.required' => '10001',
            'age.integer' => '10002',
            'height.required' => '10001',
            'height.integer' => '10002',
            'weight.required' => '10001',
            'weight.integer' => '10002',
            'bust.required' => '10001',
            'bust.integer' => '10002',
            'waistline.required' => '10001',
            'waistline.integer' => '10002',
            'hips.required' => '10001',
            'hips.integer' => '10002',
            'tags.required' => '10001',
            'tags.json' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }

        $shopGirl = $shopGirl->where('id', $id)->first();
        if(!CheckAuth::checkIfManager($shopGirl->shop_id)){
            return $this->sendFailResponse(400, 40101);
        }

        DB::beginTransaction();
        try{
            $ossClient = new OssClient(env('ALIYUN_ID'), env('ALIYUN_KEY'), env('ALIYUN_ENDPOINT'));
            $bucket = env('ALIYUN_BUCKET');

            if(!empty($request['user_id'])){
                $shopGirl->user_id = $request['user_id'];
            }
            if(isset($request['no'])){
                $shopGirl->no = $request['no'];
            }
            if($request->hasFile('cover') && $request->file('cover')->isValid()){
                $object = 'image/'.uniqid().'.'.$request->file('cover')->extension();
                $ossClient->uploadFile($bucket, $object, $request->file('cover')->getFileInfo());
                $shopGirl->cover = $object;
            }
            if(isset($request['age'])){
                $shopGirl->age = $request['age'];
            }
            if(isset($request['height'])){
                $shopGirl->height = $request['height'];
            }
            if(isset($request['weight'])){
                $shopGirl->weight = $request['weight'];
            }
            if(isset($request['bust'])){
                $shopGirl->bust = $request['bust'];
            }
            if(isset($request['waistline'])){
                $shopGirl->waistline = $request['waistline'];
            }
            if(isset($request['hips'])){
                $shopGirl->hips = $request['hips'];
            }
            if(isset($request['status'])){
                $shopGirl->status = $request['status'];
            }
            $shopGirl->save();
            if(!empty($request['tags'])){
                $request['tags'] = \GuzzleHttp\json_decode($request['tags'], true);
                $girlTag->where('girl_id', $id)->delete();
                foreach($request['tags'] as $v){
                    $girlTag->girl_id = $shopGirl->id;
                    $girlTag->tag_id = $v;
                    $girlTag->save();
                    $girlTag = new GirlTag();
                }
            }
            DB::commit();
            return $this->sendSuccessResponse([]);
        }catch(OssException $e){
            DB::rollBack();
            return $this->sendFailResponse(400,40003);
        }catch(\Exception $e){
            DB::rollBack();
            return $this->sendFailResponse(500,50000);
        }
    }
}